package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.credit_bank.mapper.LikedMapper;
import csu.web.credit_bank.pojo.Liked;
import csu.web.credit_bank.pojo.dto.ArticleCreateDto;
import csu.web.credit_bank.pojo.Articles;
import csu.web.credit_bank.service.ArticlesService;
import csu.web.credit_bank.mapper.ArticlesMapper;
import csu.web.credit_bank.utils.UUIDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author 于欣淇
 * @description 针对表【articles(文章表)】的数据库操作Service实现
 * @createDate 2025-07-01 19:12:52
 */
@Service
public class ArticlesServiceImpl extends ServiceImpl<ArticlesMapper, Articles>
        implements ArticlesService{

    @Autowired
    private UUIDManager uuidManager;
    @Autowired
    private LikedMapper likedMapper;

    public Articles createArticle(ArticleCreateDto articleDto) {
        uuidManager.generateUUID(1000);
        Articles article = new Articles();
        article.setId(uuidManager.getUUID());
        article.setAuthorId(articleDto.getAuthor());
        article.setTitle(articleDto.getTitle());
        article.setContent(articleDto.getContent());
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        article.setLikes(0);

        if (articleDto.getTags() != null && !articleDto.getTags().isEmpty()) {
            String tagsString = String.join(",", articleDto.getTags());
            article.setTags(tagsString);
        }
        this.save(article);
        return article;
    }
    public Articles getArticleById(String id) {
        return this.getById(id);
    }

    @Override
    public List<Articles> getAllArticles(String id) {
        return baseMapper.selectArticlesWithCommentCount(id);
    }

    @Override
    public void updateArticleLikes(String articleId,String userId) {
        QueryWrapper<Liked> likedQueryWrapper = new QueryWrapper<>();
        likedQueryWrapper.eq("post_id", articleId);
        likedQueryWrapper.eq("user_id", userId);
        Liked existingLikedRecord = likedMapper.selectOne(likedQueryWrapper);
        Articles article = this.getById(articleId);
        if (article == null) {
            throw new IllegalArgumentException("文章ID不存在：" + articleId);
        }
        if (existingLikedRecord != null) {
            likedMapper.delete(likedQueryWrapper);
            article.setLikes(article.getLikes() - 1);
        } else {
            Liked newLikedRecord = new Liked();
            newLikedRecord.setPostId(articleId);
            newLikedRecord.setUserId(userId);
            likedMapper.insert(newLikedRecord);
            article.setLikes(article.getLikes() + 1);
        }
        this.updateById(article);
    }
    public List<Articles> getHotArticlesInLast24Hours() {
        LocalDateTime twentyFourHoursAgo = LocalDateTime.now().minusHours(24);
        return baseMapper.getHotArticlesInLast24Hours(twentyFourHoursAgo);
    }
}




